//
//  SDTimeLineCell.m
//  GSD_WeiXin(wechat)
//
//  Created by gsd on 16/2/25.
//  Copyright © 2016年 GSD. All rights reserved.
//

/*
 
 *********************************************************************************
 *
 * GSD_WeiXin
 *
 * QQ交流群: 459274049
 * Email : gsdios@126.com
 * GitHub: https://github.com/gsdios/GSD_WeiXin
 * 新浪微博:GSD_iOS
 *
 * 此“高仿微信”用到了很高效方便的自动布局库SDAutoLayout（一行代码搞定自动布局）
 * SDAutoLayout地址：https://github.com/gsdios/SDAutoLayout
 * SDAutoLayout视频教程：http://www.letv.com/ptv/vplay/24038772.html
 * SDAutoLayout用法示例：https://github.com/gsdios/SDAutoLayout/blob/master/README.md
 *
 *********************************************************************************
 
 */

#import "SDTimeLineCell.h"
#import "SDTimeLineCellModel.h"
#import "UIView+SDAutoLayout.h"
#import "SDTimeLineCellOperationMenu.h"
#import "SDWeiXinPhotoContainerView.h"
#import "HRTimeLineCellOperationMenu.h"
#import "SDTimeLineCellOperationMenu.h"
const CGFloat contentLabelFontSize = 15;
CGFloat maxContentLabelHeight = 0; // 根据具体font而定

NSString *const kSDTimeLineCellOperationButtonClickedNotification = @"SDTimeLineCellOperationButtonClickedNotification";

@implementation SDTimeLineCell

{
    
    
    UIButton *_deleteButton;
    UIButton *_playButton;
    UILabel *_line;
    UIButton *_iconButton;
    UIImageView *_iconView;
    UILabel *_locationLable;
    UILabel *_nameLable;
    UILabel *_contentLabel;
    UILabel *_detailLabel;
    UILabel *_dateLabel;
    UILabel *_placeLabel;
    UILabel *_payLabel;
    UILabel *_sexLabel;
    UILabel *_arriveLabel;
    UILabel *_activityLabel;
    UIImageView *_videoImageView;
    SDWeiXinPhotoContainerView *_picContainerView;
    UILabel *_timeLabel;
    UIButton *_moreButton;
    SDTimeLineCellCommentView *_commentView;
    SDTimeLineCellOperationMenu *_operationMenu;
    HRTimeLineCellOperationMenu *_HRoperationMenu;
    BOOL _isTryst;
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setup];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setup {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveOperationButtonClickedNotification:) name:kSDTimeLineCellOperationButtonClickedNotification object:nil];
    _line = [UILabel new];
    _line.backgroundColor = TheLinColor;
    _iconView = [UIImageView new];
    _iconView.layer.cornerRadius = 25;
    _iconView.layer.masksToBounds = YES;
    _iconButton = [UIButton new];
    _iconButton.backgroundColor = [UIColor clearColor];
    [_iconButton addTarget:self action:@selector(clickImage:) forControlEvents:UIControlEventTouchUpInside];
    
    _deleteButton = [UIButton new];
    _deleteButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [_deleteButton setTitleColor:self.tintColor forState:UIControlStateNormal];
    [_deleteButton addTarget:self action:@selector(clickDeleteButton) forControlEvents:UIControlEventTouchUpInside];
    
    _locationLable = [UILabel new];
    _locationLable.font = [UIFont systemFontOfSize:13];
    _locationLable.textColor = [UIColor colorWithRed:(54 / 255.0) green:(71 / 255.0) blue:(121 / 255.0) alpha:0.9];
    
    _nameLable = [UILabel new];
    _nameLable.font = [UIFont systemFontOfSize:14];
    _nameLable.textColor = [UIColor colorWithRed:(54 / 255.0) green:(71 / 255.0) blue:(121 / 255.0) alpha:0.9];
    
    _contentLabel = [UILabel new];
    _contentLabel.font = [UIFont systemFontOfSize:contentLabelFontSize];
    _videoImageView = [UIImageView new];
    _contentLabel.numberOfLines = 0;
    
    if (maxContentLabelHeight == 0) {
        maxContentLabelHeight = _contentLabel.font.lineHeight * 3;
    }
    
    _detailLabel = [UILabel new];
    _detailLabel.font = [UIFont systemFontOfSize:13];
    _detailLabel.textColor = [UIColor lightGrayColor];
    _detailLabel.numberOfLines = 0;
    _payLabel = [UILabel new];
    _payLabel.font = [UIFont systemFontOfSize:13];
    _payLabel.textColor = [UIColor lightGrayColor];
    _payLabel.numberOfLines = 0;
    _sexLabel = [UILabel new];
    _sexLabel.font = [UIFont systemFontOfSize:13];
    _sexLabel.textColor = [UIColor lightGrayColor];
    _sexLabel.numberOfLines = 0;
    _arriveLabel = [UILabel new];
    _arriveLabel.font = [UIFont systemFontOfSize:13];
    _arriveLabel.textColor = [UIColor lightGrayColor];
    _arriveLabel.numberOfLines = 0;
    _dateLabel = [UILabel new];
    _dateLabel.font = [UIFont systemFontOfSize:13];
    _dateLabel.textColor = [UIColor lightGrayColor];
    _dateLabel.numberOfLines = 0;
    _activityLabel = [UILabel new];
    _activityLabel.font = [UIFont systemFontOfSize:13];
    _activityLabel.textColor = [UIColor lightGrayColor];
    _activityLabel.numberOfLines = 0;
    _placeLabel = [UILabel new];
    _placeLabel.font = [UIFont systemFontOfSize:13];
    _placeLabel.textColor = [UIColor lightGrayColor];
    _placeLabel.numberOfLines = 0;
    
    _moreButton = [UIButton new];
    [_moreButton setTitle:@"全文" forState:UIControlStateNormal];
    [_moreButton setTitleColor:TimeLineCellHighlightedColor forState:UIControlStateNormal];
    [_moreButton addTarget:self action:@selector(moreButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    _moreButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    _operationButton = [UIButton new];
    [_operationButton setImage:[UIImage imageNamed:@"Friend_news_comment_button"] forState:UIControlStateNormal];
    [_operationButton addTarget:self action:@selector(operationButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    _picContainerView = [SDWeiXinPhotoContainerView new];
    _picContainerView.typeDeleteHiden = YES;
    
    _commentView = [SDTimeLineCellCommentView new];
    _commentView.delegate = self;
    _timeLabel = [UILabel new];
    _timeLabel.font = [UIFont systemFontOfSize:13];
    
    
    _operationMenu = [SDTimeLineCellOperationMenu new];
    __weak typeof(self) weakSelf = self;
    //点赞
    [_operationMenu setLikeButtonClickedOperation:^(NSString *opertionStr){
        NSString *str = opertionStr;
        if ([weakSelf.delegate respondsToSelector:@selector(didClickLikeButtonInCell:opertionStr:)]) {
            [weakSelf.delegate didClickLikeButtonInCell:weakSelf opertionStr:str];
        }
    }];
    
    
    [_operationMenu setCommentButtonClickedOperation:^{
        if ([weakSelf.delegate respondsToSelector:@selector(didClickcCommentButtonInCell:)]) {
            [weakSelf.delegate didClickcCommentButtonInCell:weakSelf];
        }
    }];
    
    
    _HRoperationMenu = [HRTimeLineCellOperationMenu new];
//    [_HRoperationMenu setLikeButtonClickedOperation:^{
//        if ([weakSelf.delegate respondsToSelector:@selector(didClickLikeButtonInCell:)]) {
//            [weakSelf.delegate didClickLikeButtonInCell:weakSelf];
//        }
//    }];
//    [_HRoperationMenu setCommentButtonClickedOperation:^{
//        if ([weakSelf.delegate respondsToSelector:@selector(didClickcCommentButtonInCell:)]) {
//            [weakSelf.delegate didClickcCommentButtonInCell:weakSelf];
//        }
//    }];
//    [_HRoperationMenu setJoinButtonClickedOperation:^{
//        if ([weakSelf.delegate respondsToSelector:@selector(didClickcJoinButtonInCell:)]) {
//            [weakSelf.delegate didClickcJoinButtonInCell:weakSelf];
//        }
//    }];
    _playButton = [UIButton new];
    NSArray *views = @[_iconView, _nameLable, _contentLabel, _moreButton, _detailLabel, _payLabel, _sexLabel, _arriveLabel, _dateLabel, _activityLabel, _placeLabel, _picContainerView, _timeLabel, _operationButton, _operationMenu, _commentView,_HRoperationMenu,_line,_iconButton,_deleteButton,_locationLable,_videoImageView,_playButton];
    
    [self.contentView sd_addSubviews:views];
    
    UIView *contentView = self.contentView;
    CGFloat margin = 10;
    
    _iconView.sd_layout
    .leftSpaceToView(contentView, margin)
    .topSpaceToView(contentView, margin + 5)
    .widthIs(50)
    .heightIs(50);
    
    _iconButton.sd_layout
    .leftSpaceToView(contentView, margin)
    .topSpaceToView(contentView, margin + 5)
    .widthIs(50)
    .heightIs(50);
    
    _nameLable.sd_layout
    .leftSpaceToView(_iconView, margin)
    .topEqualToView(_iconView)
    .heightIs(18);
    [_nameLable setSingleLineAutoResizeWithMaxWidth:200];
    
    _contentLabel.sd_layout
    .leftEqualToView(_nameLable)
    .topSpaceToView(_nameLable, margin)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    // morebutton的高度在setmodel里面设置
    _moreButton.sd_layout
    .leftEqualToView(_contentLabel)
    .topSpaceToView(_contentLabel, 0)
    .widthIs(30);
    
    _detailLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _payLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _sexLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _arriveLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _dateLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _activityLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _placeLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _playButton.sd_layout
    .leftEqualToView(_nameLable);
    
    _picContainerView.sd_layout
    .leftEqualToView(_contentLabel); // 已经在内部实现宽度和高度自适应所以不需要再设置宽度高度，top值是具体有无图片在setModel方法中设置
    
    _timeLabel.sd_layout
    .leftEqualToView(_contentLabel)
    .topSpaceToView(_picContainerView, margin)
    .heightIs(15);
    [_timeLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    _locationLable.sd_layout
    .rightSpaceToView(contentView, margin)
    .leftEqualToView(_contentLabel);
    
    _deleteButton.sd_layout
    .leftSpaceToView(_timeLabel, 10)
    .topEqualToView(_timeLabel)
    .widthIs(80)
    .heightIs(15);
    
    _operationButton.sd_layout
    .rightSpaceToView(contentView, margin)
    .centerYEqualToView(_timeLabel)
    .heightIs(25)
    .widthIs(25);
    
    _commentView.sd_layout
    .leftEqualToView(_contentLabel)
//    .leftSpaceToView(contentView, margin)

    .rightSpaceToView(self.contentView, margin)
    .topSpaceToView(_locationLable, margin); // 已经在内部实现高度自适应所以不需要再设置高度
    
    _operationMenu.sd_layout
    .rightSpaceToView(_operationButton, 0)
    .heightIs(36)
    .centerYEqualToView(_operationButton)
    .widthIs(0);
    
    _HRoperationMenu.sd_layout
    .rightSpaceToView(_operationButton, 0)
    .heightIs(36)
    .centerYEqualToView(_operationButton)
    .widthIs(0);
    
    _line.sd_layout
    .leftEqualToView(contentView)
    .rightEqualToView(contentView)
    .heightIs(1);
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setModel:(Active *)model {
    _model = model;

    
    if ([@(model.uid).stringValue isEqualToString:[HQHttpManager getUserId]]) {
        _deleteButton.hidden = NO;
    } else {
        _deleteButton.hidden = YES;
    }
    [_operationMenu setupWithLikeOperationMenu:model.praiseList];

    [_commentView setupWithLikeItemsArray:model.praiseList commentItemsArray:model.commentList];
    if(model.photo){
       [_iconView sd_setImageWithURL:[NSURL URLWithString:HttpImgUrl(model.photo)] placeholderImage:[UIImage imageNamed:@"llustration"]];
    }
    _nameLable.text = model.nickName;
    // 防止单行文本label在重用时宽度计算不准的问题
   // [_nameLable sizeToFit];
    
    
    switch (model.trystType) {
        case 1:
            _contentLabel.text = @"约人吃饭";
            _isTryst = YES;
            break;
        case 2:
            _contentLabel.text = @"约人看电影";
            _isTryst = YES;
            break;
        case 3:
            _contentLabel.text = @"约人唱歌";
            _isTryst = YES;
            break;
        case 4:
            _contentLabel.text = @"约人运动";
            _isTryst = YES;
            break;
        default:
            _contentLabel.text = model.content;
            _isTryst = NO;
            break;
    }
    
    _detailLabel.text = model.hopeNote;
    switch (model.whoPay) {
        case 1:
            _payLabel.text = @"AA";
            break;
        case 2:
            _payLabel.text = @"我买单";
            break;
        case 3:
            _payLabel.text = @"对方买单";
            break;
        default:
            _payLabel.text = @"";
            break;
    }
    
    switch (model.hopeSex) {
        case 3:
            _sexLabel.text = @"仅限女士";
            break;
        case 1:
            _sexLabel.text = @"仅限男士";
            break;
        case 2:
            _sexLabel.text = @"男女不限";
            break;
        default:
            _sexLabel.text = @"";
            break;
    }
    switch (model.howArrive) {
        case 1:
            _arriveLabel.text = @"我接对方";
            break;
        case 2:
            _arriveLabel.text = @"对方接我";
            break;
        case 3:
            _arriveLabel.text = @"自己到达";
            break;
        default:
            _arriveLabel.text = @"";
            break;
    }
    _dateLabel.text = model.trystTime;
    _activityLabel.text = model.funName;
    _placeLabel.text = model.trystPlace;
    if (model.video.length) {
        _picContainerView.picPathStringsArray = @[];
    } else {
        _picContainerView.picPathStringsArray = [model.img componentsSeparatedByString:@","];
    }
    
    if (model.shouldShowMoreButton) { // 如果文字高度超过60
        _moreButton.sd_layout.heightIs(20);
        _moreButton.hidden = NO;
        if (model.isOpening) { // 如果需要展开
            _contentLabel.sd_layout.maxHeightIs(MAXFLOAT);
            [_moreButton setTitle:@"收起" forState:UIControlStateNormal];
        } else {
            _contentLabel.sd_layout.maxHeightIs(maxContentLabelHeight);
            [_moreButton setTitle:@"全文" forState:UIControlStateNormal];
        }
    } else {
        _moreButton.sd_layout.heightIs(0);
        _moreButton.hidden = YES;
    }
    if (model.hopeNote.length) {
        _detailLabel.sd_layout.topSpaceToView(_moreButton, 10.0);
        _detailLabel.hidden = NO;
    } else {
        _detailLabel.sd_layout.topSpaceToView(_moreButton, 0.0);
        _detailLabel.sd_layout.heightIs(0);
        _detailLabel.hidden = YES;
    }
    if (_payLabel.text.length) {
        _payLabel.sd_layout.topSpaceToView(_detailLabel, 10.0);
        _payLabel.hidden = NO;
    } else {
        _payLabel.sd_layout.topSpaceToView(_detailLabel, 0.0);
        _payLabel.sd_layout.heightIs(0);
        _payLabel.hidden = YES;
    }
    if (_sexLabel.text.length) {
        _sexLabel.sd_layout.topSpaceToView(_payLabel, 10.0);
        _sexLabel.hidden = NO;
    } else {
        _sexLabel.sd_layout.topSpaceToView(_payLabel, 0.0);
        _sexLabel.sd_layout.heightIs(0);
        _sexLabel.hidden = YES;
    }
    if (_arriveLabel.text.length) {
        _arriveLabel.sd_layout.topSpaceToView(_sexLabel, 10.0);
        _arriveLabel.hidden = NO;
    } else {
        _arriveLabel.sd_layout.topSpaceToView(_sexLabel, 0.0);
        _arriveLabel.sd_layout.heightIs(0);
        _arriveLabel.hidden = YES;
    }
    if (_dateLabel.text.length) {
        _dateLabel.sd_layout.topSpaceToView(_arriveLabel, 10.0);
        _dateLabel.hidden = NO;
    } else {
        _dateLabel.sd_layout.topSpaceToView(_arriveLabel, 0.0);
        _dateLabel.sd_layout.heightIs(0);
        _dateLabel.hidden = YES;
    }
    if (_activityLabel.text.length) {
        _activityLabel.sd_layout.topSpaceToView(_dateLabel, 10.0);
        _activityLabel.hidden = NO;
    } else {
        _activityLabel.sd_layout.topSpaceToView(_dateLabel, 0.0);
        _activityLabel.sd_layout.heightIs(0);
        _activityLabel.hidden = YES;
    }
    if (_placeLabel.text.length) {
        _placeLabel.sd_layout.topSpaceToView(_activityLabel, 10.0);
        _placeLabel.hidden = NO;
    } else {
        _placeLabel.sd_layout.topSpaceToView(_activityLabel, 0.0);
        _placeLabel.sd_layout.heightIs(0);
        _placeLabel.hidden = YES;
    }
    //播放器
    CGFloat videoTopMargin = 0;
    if (model.video.length) {
        videoTopMargin = 10;
        [_playButton setImage:[UIImage imageNamed:@"home_play_icon"] forState:UIControlStateNormal];
        _playButton.sd_layout
        .heightIs(180)
        .widthIs(120)
        .topSpaceToView(_placeLabel,videoTopMargin);
        _videoImageView.sd_layout
        .topEqualToView(_playButton)
        .bottomEqualToView(_playButton)
        .leftEqualToView(_playButton)
        .rightEqualToView(_playButton);
        if(model.img){
            [_videoImageView sd_setImageWithURL:[NSURL URLWithString:HttpImgUrl(model.img)] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        }
        _playButton.hidden = NO;
        [_playButton addTarget:self action:@selector(clickPlayButton) forControlEvents:UIControlEventTouchUpInside];
    } else {
        _playButton.sd_layout
        .heightIs(0)
        .widthIs(120)
        .topSpaceToView(_placeLabel,videoTopMargin);
        _playButton.hidden = YES;
    }
    CGFloat picContainerTopMargin = 0;
    if (_picContainerView.picPathStringsArray.count) {
        picContainerTopMargin = 10;
    }
    _picContainerView.sd_layout.topSpaceToView(_playButton, picContainerTopMargin);
    
    UIView *bottomView;
    
    CGFloat locationTopMargin = 0;
    if (model.location.length) {
        locationTopMargin = 10;
        _locationLable.text = model.location;
        _locationLable.sd_layout
        .heightIs(15)
        .topSpaceToView(_timeLabel,locationTopMargin);
        _locationLable.hidden = NO;
    } else {
        _locationLable.sd_layout
        .heightIs(0)
        .topSpaceToView(_timeLabel,locationTopMargin);
        _locationLable.hidden = YES;
    }
    if (!model.commentList.count && !model.praiseList.count) {
        if (model.location.length) {
            bottomView = _locationLable;
        } else {
            bottomView = _timeLabel;
        }
    } else {
        bottomView = _commentView;
    }
    _line.sd_layout.topSpaceToView(bottomView, 14);
    [self setupAutoHeightWithBottomView:bottomView bottomMargin:15];
    
    _timeLabel.text = model.createTime;
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    if (_operationMenu.isShowing) {
        _operationMenu.show = NO;
    }
//    if (_HRoperationMenu.isShowing) {
//        _HRoperationMenu.show = NO;
//    }
}

- (void)clickDeleteButton {
    WEAKSELF
    if ([weakSelf.delegate respondsToSelector:@selector(didClickDeleteInCell:)]) {
        [weakSelf.delegate didClickDeleteInCell:weakSelf];
    }
}

- (void)clickImage:(UIButton *)sender {
    WEAKSELF
    if ([weakSelf.delegate respondsToSelector:@selector(didClickIconInCell:)]) {
        [weakSelf.delegate didClickIconInCell:weakSelf];
    }
}

#pragma mark - private actions
- (void)moreButtonClicked {
    if (self.moreButtonClickedBlock) {
        self.moreButtonClickedBlock(self.indexPath);
    }
}
#pragma mark =========点赞==========
- (void)operationButtonClicked {
    [self postOperationButtonClickedNotification];

    
    if (_isTryst&&![@(_model.uid).stringValue isEqualToString:[HQHttpManager getUserId]]) {
//        [self.contentView bringSubviewToFront:_HRoperationMenu];
//        _HRoperationMenu.show = !_HRoperationMenu.isShowing;
    } else {
        
        [self.contentView bringSubviewToFront:_operationMenu];
        _operationMenu.show = !_operationMenu.isShowing;
    }
}

- (void)receiveOperationButtonClickedNotification:(NSNotification *)notification {
    UIButton *btn = [notification object];
    if (btn != _operationButton && _operationMenu.isShowing) {
        _operationMenu.show = NO;
    }
//    if (btn != _operationButton && _HRoperationMenu.isShowing) {
//        _HRoperationMenu.show = NO;
//    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self postOperationButtonClickedNotification];
    if (_operationMenu.isShowing) {
        _operationMenu.show = NO;
    }
//    if (_HRoperationMenu.isShowing) {
//        _HRoperationMenu.show = NO;
//    }
}

- (void)postOperationButtonClickedNotification {
    [[NSNotificationCenter defaultCenter] postNotificationName:kSDTimeLineCellOperationButtonClickedNotification object:_operationButton];
}
//播放代理
- (void)clickPlayButton {
    WEAKSELF
    if ([weakSelf.delegate respondsToSelector:@selector(didClickPlayButton:)]) {
        [weakSelf.delegate didClickPlayButton:weakSelf.model.video];
    }
}
//点击评论 回复

- (void)replycommentId:(NSString *)commentid username:(NSString *)username{
    NSLog(@"%@=========+++++++++++============%@",commentid,username);

    self.didClickCommentLabelBlock(commentid, CGRectMake(0, 0, 0,0), _indexPath,username);
}

@end
